package com.qidianit.mes.production.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.pojo.Workmanship;
import com.qidianit.mes.production.mapper.WorkmanshipMapper;
import com.qidianit.mes.production.service.WorkmanshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author djl
 * @Time 2024/10/19 9:58
 */
@Service
public class WorkmanshipServiceImpl implements WorkmanshipService {
    @Autowired
    private WorkmanshipMapper workmanshipMapper;
    @Override
    public MESResult list(String workmanshipRouteId, String workmanshipName, Integer workmanshipStatus, int page, int limit) {
        // 开启分页  参数1： 页码  参数2： 每页大小
        PageHelper.startPage(page,limit);
        // 查询全部
        List<Workmanship> processes = workmanshipMapper.list(workmanshipRouteId,workmanshipName,workmanshipStatus);
        PageInfo<Workmanship> pageInfo = new PageInfo<>(processes);
        // 将pageInfo对象设置到qdResult中
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addNewWorkmanship(Workmanship workmanship) {
        if(workmanshipMapper.selectByWorkmanshipRouteId(workmanship.getWorkmanshipRouteId()) == null){
            workmanshipMapper.addNewWorkmanship(workmanship);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
    }

    @Override
    public MESResult deleteWorkmanship(String workmanshipRouteId) {
        return MESResult.success(200,"OK",workmanshipMapper.deleteWorkmanship(workmanshipRouteId));
    }

    @Override
    public MESResult editWorkmanship(Workmanship workmanship) {
        if(workmanshipMapper.selectByWorkmanshipRouteId(workmanship.getWorkmanshipRouteId()).getWorkmanshipId() == workmanship.getWorkmanshipId()
         || workmanshipMapper.selectByWorkmanshipRouteId(workmanship.getWorkmanshipRouteId()) == null){
            workmanshipMapper.editWorkmanship(workmanship);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
    }

    @Override
    public MESResult reDelete(String[] workmanshipRouteIds) {
        System.out.println("impl");
        return MESResult.success(200,"OK",workmanshipMapper.reDelete(workmanshipRouteIds));
    }

    @Override
    public MESResult showByWorkmanshipId(Integer workmanshipId) {
        return MESResult.success(workmanshipMapper.showByWorkmanshipId(workmanshipId));
    }
}
